from xml.sax.handler import ContentHandler

from tweak import Tweak


class TempHandler(ContentHandler):
	'''class to handle template xml files.'''
	
	def __init__(self):
		self.inTemplate = 0
		self.inInstall = 0
		self.inTweak = 0
		self.toInstall = [ ]
		self.toTweak = [ ]
	
	def startElement(self, name, attrs):
		self.text = ''
		if name == 'template':
			self.inTemplate = 1
		elif name == 'install':
			self.inInstall = 1
			self.version = None
			attr_dict = dict(attrs)
			if 'version' in attr_dict.keys():
				self.version = attr_dict['version']
		elif name == 'tweak':
			self.inTweak = 1
	
	def endElement(self, name):
		if name == 'template':
			self.inTemplate = 0
		elif name == 'install':
			self.inInstall = 0
			self.toInstall.append((self.text, self.version))
		elif name == 'tweak':
			self.inTweak = 0
			self.toTweak.append(self.text)
	
	def characters(self, ch):
		self.text += ch
			
class TweakHandler(ContentHandler):
	'''class to handle template xml files.'''
	
	def __init__(self):
		self.tweaks = [ ]
	
	def startElement(self, name, attrs):
		if not name == 'tweak':
			return
		attr_dict = dict(attrs)
		tweak_file = attr_dict['file']
		tweak_desc = attr_dict['description']
		self.tweaks.append((tweak_file, tweak_desc))
		
		
	
